/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author frederic
 */
import java.io.*;

public class PersistenceMgr2 {
    
        private String nomFichierVehicules;
        private String nomFichierClients;
        private String nomFichierLocations;
	
    
	public PersistenceMgr2(String nomFichierVehicules, String nomFichierClients, String nomFichierLocations) {
            this.nomFichierVehicules = nomFichierVehicules;
            this.nomFichierClients = nomFichierClients;
            this.nomFichierLocations = nomFichierLocations;
        }
	
	public Vehicule[] donneLesVehicules() {
        
            Vehicule[] vehicules = this.litFichierVehicule();
		
            return vehicules;
		
        }
        
        public Location[] donneLesLocations() {
        
            Location[] locations = this.litFichierLocation();
		
            return locations;
		
        }
        
        
        private Client decodeLigneClient(String ligne) {
            if (ligne == null || ligne.length() == 0) return null;
            String[] temp = ligne.split(";");
            if (temp.length != 3) return null;
            return new Client(Integer.parseInt(temp[0]), temp[1], Double.parseDouble(temp[2]));
        }
        
        private Location decodeLigneLocation(String ligne) {
            if (ligne == null || ligne.length() == 0) return null;

            String[] temp = ligne.split(";");

            if (temp.length != 5) return null;
            
            
            // on trouve le client correspondant à l'id_client (temp[1])
            
            Client client = null;
            
            for (Client client_temp : this.litFichierClient()) {
                if (client_temp.getId_client() == Integer.parseInt(temp[1])) {
                    client = client_temp;
                    break;
                }
            }
            
            // on trouve le véhicule correspondant à l'id_vehicule (temp[2])
            
            Vehicule vehicule = null;
            
            for (Vehicule vehicule_temp : this.litFichierVehicule()) {
                if (vehicule_temp.getId_vehicule() == Integer.parseInt(temp[2])) {
                    vehicule = vehicule_temp;
                    break;
                }
            }
            
            return new Location(Integer.parseInt(temp[0]), client, vehicule, Integer.parseInt(temp[3]), this.decodeDate(temp[4]));

        }

       private Vehicule decodeLigneVehicule(String ligne) {       
            if (ligne == null || ligne.length() == 0) return null;

            String[] temp = ligne.split(";");

            if (temp.length != 6) return null;

            if ( temp[1].equals("1")) { 
                Vehicule vehicule = new Utilitaire(Integer.parseInt(temp[0]), temp[2],Integer.parseInt(temp[3]), temp[4], Double.parseDouble(temp[5] ));

                return vehicule;
            }
            else if ( temp[1].equals("2")) {
                Vehicule vehicule = new Luxe(Integer.parseInt(temp[0]), temp[2],Integer.parseInt(temp[3]), temp[4], Double.parseDouble(temp[5] ));

                return vehicule;
            }
            else if ( temp[1].equals("3")) {
                Vehicule vehicule = new Citadine(Integer.parseInt(temp[0]), temp[2],Integer.parseInt(temp[3]), temp[4]);

                return vehicule;
            }
            return null;
       }
       
       private Location[] litFichierLocation() {
           Location[] locations = new Location[1000];
           Location temp = null;
           
            int i = 0;

            try {
                BufferedReader in = new BufferedReader(new FileReader(this.nomFichierLocations));

                String str;
                while ((str = in.readLine()) != null) {                
                    temp = this.decodeLigneLocation(str);
                    if (temp != null) {                    
                        locations[i] = temp;
                        i++;
                    }

                }

                in.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

            Location[] finalLocations = new Location[i];

            for (int j = 0; j < i ; j++) {
                finalLocations[j] = locations[j];            
            }
            
            // debug :
            for (Location location : finalLocations) {
                System.out.println("");
            }
            

            return finalLocations;
       
       }
       
       private Client[] litFichierClient() {
           Client[] clients = new Client[1000];
           Client temp = null;
           
            int i = 0;

            try {
                BufferedReader in = new BufferedReader(new FileReader(this.nomFichierClients));

                String str;
                while ((str = in.readLine()) != null) {                
                    temp = this.decodeLigneClient(str);
                    if (temp != null) {                    
                        clients[i] = temp;
                        i++;
                    }

                }

                in.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

            Client[] finalClients = new Client[i];

            for (int j = 0; j < i ; j++) {
                finalClients[j] = clients[j];            
            }

            return finalClients;
           
       }

       private Vehicule[] litFichierVehicule() {

            Vehicule[] vehicules = new Vehicule[1000]; // ne fonctionnera que pour les fichiers de 1000 personnes maximum

            Vehicule temp = null;

            int i = 0;

            try {
                BufferedReader in = new BufferedReader(new FileReader(this.nomFichierVehicules));

                String str;
                while ((str = in.readLine()) != null) {                
                    temp = this.decodeLigneVehicule(str);
                    if (temp != null) {                    
                        vehicules[i] = temp;
                        i++;
                    }

                }

                in.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

            Vehicule[] finalVehicules = new Vehicule[i];

            for (int j = 0; j < i ; j++) {
                finalVehicules[j] = vehicules[j];            
            }

            return finalVehicules;

        }
       
       
	private Date decodeDate(String champ) {
		String[] temp = champ.split("/");
		if (temp.length != 3) return new Date (0, 0, 0);
		else return new Date(Integer.parseInt(temp[0]), Integer.parseInt(temp[1]), Integer.parseInt(temp[2]));
	}

}
